/**
 * Copyright 2015 lerry
 * 
 * All right reserved.
 * 
 * Apr 1, 2015
 */
package com.bac.bacplatform;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Response.Listener;
import com.bac.utils.Config;
import com.bac.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * @author lerry
 * 
 */
public class PayPasswordActivity extends BaseActivity {

	private EditText mEditTextPwd;

	private ImageButton mImageClear;

	private Intent mIntent;

	private String carhao, money, total, ensure;
	private int monthNum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pay_password);
		mIntent = getIntent();
		String card = mIntent.getStringExtra("cardNum");
		String[] cardArray = card.split(" ");
		carhao = cardArray[0];
		money = mIntent.getStringExtra("money_text");
		monthNum = Integer.parseInt(mIntent.getStringExtra("monthNum"));
		ensure = String.format("%.2f", monthNum*Double.parseDouble(money));//""+ (monthNum*Double.parseDouble(money));
		total = mIntent.getStringExtra("total");

		Log.i(getClass().getName() + "-----------", "param:[" + "carhao:"
				+ carhao + " money:" + money + " monthNum:" + monthNum + " ensure:" + ensure + " total: " + total+ "]");
	}

	@Override
	public void init() {

		mEditTextPwd = (EditText) findViewById(R.id.pwd_edit);

		mImageClear = (ImageButton) findViewById(R.id.btn_clear_idcard_number);

		mImageClear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mEditTextPwd.setText("");
			}
		});

		TextWatcher watcher = new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (mEditTextPwd.hasFocus()
						&& mEditTextPwd.getText().toString().trim().length() > 0) {
					mImageClear.setVisibility(View.VISIBLE);
				} else {
					mImageClear.setVisibility(View.INVISIBLE);
				}
			}
		};

		mEditTextPwd.addTextChangedListener(watcher);

		findViewById(R.id.bt_ok).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mEditTextPwd.getText().toString().length() == 0) {
					showToast("请输入支付密码");
				} else {
					payForJuyoujin();
				}
			}
		});

		findViewById(R.id.title_left).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finishActivityOut();
			}
		});
	}

	/**
	 * 揩油宝支付
	 */
	public void payForJuyoujin() {

		Listener<String> sListener = new Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i(getClass().getName(), response.toString());

				HashMap<String, String> result = new Gson().fromJson(response,
						new TypeToken<HashMap<String, String>>() {
						}.getType());
				if (result != null && result.containsKey("code")) {
					if (result.get("code").equals("3")) {
						AlertDialog.Builder builder = new AlertDialog.Builder(
								PayPasswordActivity.this, R.style.Custom_dialog);
						final AlertDialog alertDialog = builder.create();
						alertDialog.show();
						Window window = alertDialog.getWindow();
						View view = LayoutInflater.from(
								PayPasswordActivity.this).inflate(
								R.layout.dialog_alert, null);
						((TextView) (view.findViewById(R.id.tv_title)))
								.setText("提示");
						((TextView) (view.findViewById(R.id.tv_message)))
								.setText("支付失败");
						view.findViewById(R.id.ly_ok).setOnClickListener(
								new OnClickListener() {
									@Override
									public void onClick(View v) {
										alertDialog.dismiss();
//										finishActivityOut();
									}
								});
						window.setContentView(view);
						window.setLayout(
								(int) (Util.getWidth(getApplicationContext()) * 0.8),
								WindowManager.LayoutParams.WRAP_CONTENT);
					} else {
						Intent intent = new Intent(PayPasswordActivity.this,
								PayResultActivityone.class);
						intent.putExtras(mIntent.getExtras());
						startActivityIn(intent);
					}
				}
			}
		};
		showLoadingProgress();
		String path = Config.URL_PAY_JUYOUJIN;
		Map<String, String> map = new HashMap<String, String>();
		map.put("amount", ensure);
		map.put("account", carhao);
		map.put("paymentPassword", mEditTextPwd.getText().toString());
		map.put("terminalId", Util.getDeviceID(PayPasswordActivity.this));
		if (monthNum == 1) {
			map.put("transactionCode", "GAS001");
		} else {
			map.put("transactionCode", "GAS004");
			map.put("productCode", "" + monthNum);
		}

		Util.httpStringPost(path, sListener, this, map);

	}
}
